#ifndef STRINGWHEELWIDGET_H
#define STRINGWHEELWIDGET_H

#include <QWidget>
#include <QStringList>
#include <QPropertyAnimation>

class StringWheelWidget : public QWidget
{
    Q_OBJECT
    Q_PROPERTY(double currentPosition READ currentPosition WRITE setCurrentPosition)

public:
    explicit StringWheelWidget(QWidget *parent = nullptr);

    void setItems(const QStringList &items);
    QStringList items() const;

    int currentIndex() const;
    void setCurrentIndex(int index);
    QString currentText() const;

    void setOrientation(Qt::Orientation orientation);
    Qt::Orientation orientation() const;

    // --- 新增的核心接口 ---
    // 设置屏幕内同时显示多少项（例如：设置为 5，则自动计算行高，使得屏幕刚好容纳 5 行）
    void setVisibleItemCount(int count);
    int visibleItemCount() const;

signals:
    void currentIndexChanged(int index);
    void currentTextChanged(const QString &text);

protected:
    void paintEvent(QPaintEvent *event) override;
    void wheelEvent(QWheelEvent *event) override;
    void mousePressEvent(QMouseEvent *event) override;
    void mouseMoveEvent(QMouseEvent *event) override;
    void mouseReleaseEvent(QMouseEvent *event) override;
    void resizeEvent(QResizeEvent *event) override;

private:
    double currentPosition() const;
    void setCurrentPosition(double pos);
    void snapToTarget();
    void recalcItemSize(); // 内部重新计算尺寸

private:
    QStringList m_items;
    Qt::Orientation m_orientation;
    int m_itemSize;          // 单项尺寸（高度或宽度），由计算得出
    int m_visibleItemCount;  // 显示数量

    double m_currentPosition;
    bool m_isDragging;
    int m_lastMousePos;

    QPropertyAnimation *m_snapAnimation;
};

#endif // STRINGWHEELWIDGET_H
